import Vue from 'vue'
import VueRouter from 'vue-router'

const login = () => import('@/views/login/login.vue')
const layout = () => import('@/views/layout/layout.vue')
const mananger = () => import('@/views/mananger/mananger.vue')
const payment = () => import('@/views/payment/pay.vue')
const productionDetail = () => import('@/views/productionDetail/productionDetail.vue')
const search = () => import('@/views/search/search.vue')
const searchList = () => import('@/views/searchList/searchList.vue')
const not404 = () => import('@/views/not404.vue')
const comment = () => import('@/views/comment/comment.vue')

import home from '@/views/home/home.vue'
import clas from '@/views/class/class.vue'
import cart from '@/views/cart/cart.vue'
import mine from '@/views/mine/mine.vue'



import store from '@/store/index.js'

Vue.use(VueRouter)

const router = new VueRouter({
	routes: [
		{ path: '/login', component: login },
		{
			path: '/layout', component: layout, children: [
				{ path: '/home', component: home, name: 'home' },
				{ path: '/class', component: clas },
				{ path: '/cart', component: cart },
				{ path: '/mine', component: mine }
			]
		},
		{ path: '/mananger', component: mananger },
		{ path: '/payment', component: payment },
		{ path: '/productionDetail/:id', component: productionDetail },
		{ path: '/payment', component: payment },
		{ path: '/search', component: search },
		{ path: '/searchList', component: searchList },
		{ path: '/comment/:id', component: comment },
		{ path: '/', redirect: '/home' },
		{ path: '*', component: not404 }
	]
})

const authUrls = ['/payment', '/mananger']

router.beforeEach((to, from, next) => {
	if (authUrls.includes(to.path) && store.getters.token === false) return next('/login')
	next()
})

export default router
